package com.budwk.app.sqfwV2.service.centuryCity;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfw.services.SqfwFormsummaryNewService;
import com.budwk.app.sqfwDoc.bo.ExcelCreateConfig;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.Sqfw2ExcelGPForController;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.ResultOfExcelGenerator;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityOldCarePdfDTO;
import com.budwk.app.sqfwV2.dto.centuryCity.CenturyCityProblemParamDTO;
import com.budwk.app.sqfwV2.enums.centuryCityOldCare.CenturyCityOldCareV2FileEnums;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author ZY.Wang
 * @since 2024/6/8
 */
@IocBean
public class CenturyCityOldCareServiceImpl implements CenturyCityOldCareService {

    @Inject
    private CenturyCityOldCareExcelService centuryCityOldCareExcelService;
    @Inject
    private SqfwFormsummaryNewService sqfwFormsummaryNewService;

    @Override
    public Map<String, Object> getDownloadFileDetail(String userId, Date startDate, Date endDate) {

        endDate = DateUtil.endOfDay(endDate);
        Cnd cnd = Cnd.NEW();
        cnd.and(Sqfw_formSummary_new::getFillDate, ">=", startDate)
                .and(Sqfw_formSummary_new::getFillDate, "<=", endDate)
                .and(Sqfw_formSummary_new::getCreateBy, "=", userId)
                .and(Sqfw_formSummary_new::getType, "=", "养老服务站巡查");
        List<Sqfw_formSummary_new> summaryNewList = sqfwFormsummaryNewService.query(cnd);
        List<String> formSummaryIds = summaryNewList.stream().map(Sqfw_formSummary_new::getId).collect(Collectors.toList());

        // 在这里进行一些判断看一下哪几个pdf能够下载
        boolean pdf1HaveData = summaryNewList.size() > 0;
        boolean foodSalesHaveData = false;
        boolean foodDailyHaveData = false;
        for (Sqfw_formSummary_new formSummaryNew : summaryNewList) {
            String paramDataStr = formSummaryNew.getParamData();
            CenturyCityOldCarePdfDTO paramData = JSONObject.parseObject(paramDataStr, CenturyCityOldCarePdfDTO.class);

            List<CenturyCityProblemParamDTO> foodServiceForm = paramData.getFoodServiceForm();
            List<CenturyCityProblemParamDTO> foodSaleForm = paramData.getFoodSaleForm();

            if (foodServiceForm != null) foodDailyHaveData = true;
            if (foodSaleForm != null) foodSalesHaveData = true;
        }

        Map<String, CenturyCityDownFileTypesResVO> resultMap = Maps.newHashMap();
        for (CenturyCityOldCareV2FileEnums fileEnum : CenturyCityOldCareV2FileEnums.values()) {

            String fileType = fileEnum.getFileType();
            String fileTypeFront = fileType.equals("docx") ? "pdf" : "excel";

            CenturyCityDownFileTypesResVO fileVO = new CenturyCityDownFileTypesResVO();
            fileVO.setFileName(fileEnum.getName() + "." + fileEnum.getFileType());

            // 是否能够下载pdf检测
            if (fileEnum.getCode().equals(CenturyCityOldCareV2FileEnums.elderlyCareInsSafetyCheckPdf.getCode())) {
                fileVO.setHiveData(pdf1HaveData);
            }

            if (fileEnum.getCode().equals(CenturyCityOldCareV2FileEnums.foodSalesDailyInsPdf.getCode())) {
                fileVO.setHiveData(foodSalesHaveData);
            }

            if (fileEnum.getCode().equals(CenturyCityOldCareV2FileEnums.foodServiceDailyInsPdf.getCode())) {
                fileVO.setHiveData(foodDailyHaveData);
            }

            fileVO.setFileCode(fileEnum.getCode());
            fileVO.setFileType(fileTypeFront);
            fileVO.setFilePath(fileEnum.getTemplatePath());
            fileVO.setLabel("台账");
            fileVO.setFileTemplatePath(fileEnum.getTemplatePath());
            fileVO.setFileName(fileVO.getFileName().replace("docx", "pdf"));
            fileVO.setPreviewImage(Lists.newArrayList(fileEnum.getPreviewPath()));
            resultMap.put(fileEnum.getCode(), fileVO);
        }

        Sqfw2ExcelGPForController excelGP = new Sqfw2ExcelGPForController();
        excelGP.setFormSummaryIdList(formSummaryIds);
        excelGP.setUserId(userId);

        List<ExcelCreateConfig> excelCreateConfigList = Lists.newArrayList();
        for (String fileIdentity : resultMap.keySet()) {
            CenturyCityDownFileTypesResVO downFileTypesResVO = resultMap.get(fileIdentity);

            String fileType = downFileTypesResVO.getFileType();
            if (!fileType.equals("excel")) continue;

            ExcelCreateConfig excelCreateConfig = new ExcelCreateConfig();
            excelCreateConfig.setFileIdentity(downFileTypesResVO.getFileCode());
            excelCreateConfig.setCreateExcelFileFlag(false);
            excelCreateConfigList.add(excelCreateConfig);
        }

        excelGP.setExcelCreateConfigList(excelCreateConfigList);
        excelGP.setFillStartDate(startDate);
        excelGP.setFillEndDate(endDate);

        // 检查excel能够生成
        List<ResultOfExcelGenerator> excelCreateList = centuryCityOldCareExcelService.createExcel(excelGP);
        for (ResultOfExcelGenerator result : excelCreateList) {
            String fileIdentity = result.getFileIdentity();

            CenturyCityDownFileTypesResVO downFileTypeRes = resultMap.get(fileIdentity);
            downFileTypeRes.setHiveData(result.getPassCreateConditionFlag());

            Map<String, Object> customerParamMap = result.getCustomerParamMap();
            if (customerParamMap != null) {
                List<String> couldDownloadIds = (List<String>) customerParamMap.get("couldDownloadIds");
                downFileTypeRes.setIds(couldDownloadIds);
            }
        }

        List<CenturyCityDownFileTypesResVO> resultList = Lists.newArrayList();
        for (String fileIdentity : resultMap.keySet()) {
            resultList.add(resultMap.get(fileIdentity));
        }

        Map<String, Object> resultDataMap = Maps.newHashMap();
        resultDataMap.put("totalCount", summaryNewList.size());
        resultDataMap.put("list", resultList);

        return resultDataMap;
    }
}
